﻿''' <summary>
''' Display table containing graph series data, WRDB data source data (e.g., BMD data), or shapefile attribute data
''' </summary>
''' <remarks></remarks>
Public Class frmTable
    Private dgvTable As WRDB.Controls.DGVEditor

    Private GraphTab As clsGraphTab = Nothing
    Private DataSource As WRDB.DataSource.clsData = Nothing

    Private Sub frmTable_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        SaveWindowPos(REGAPP, Me)
        TableForm = Nothing
    End Sub

    Private Sub frmTable_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            GetWindowPos(REGAPP, Me)
            dgvTable = New WRDB.Controls.DGVEditor(dgTable)
            Refresh()
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Friend Sub SetGraphTab(ByRef _GraphTab As clsGraphTab)
        GraphTab = _GraphTab
    End Sub

    Friend Sub SetDataSource(ByRef _DataSource As WRDB.DataSource.clsData)
        DataSource = _DataSource
    End Sub

    Friend Overloads Sub Refresh()
        With dgTable
            .Columns.Clear()
            .Rows.Clear()
            .ReadOnly = True
            .AllowUserToOrderColumns = False
            .ColumnHeadersVisible = True
            .RowHeadersVisible = True
            .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .DefaultCellStyle.Font = New Font(.DefaultCellStyle.Font.FontFamily, 8)
            .ColumnHeadersDefaultCellStyle.Font = New Font(.DefaultCellStyle.Font, FontStyle.Bold)

            'This compiler directive is set in My Project | Compile | Advanced Compile Options | Custom Constants; set it like this: Project="GRAPH"
            'This allows the shared form (frmTable) to display data depending on whether it is compiled for Graph or EPDRiv1

            If GraphTab IsNot Nothing AndAlso GraphTab.Series IsNot Nothing Then
                'for Graph, get data from currently active graph
                For i As Integer = 0 To GraphTab.Series.Count - 1
                    Dim ser As clsSeries = GraphTab.Series(i)
                    AddSeries(ser.ZedCurve, "Series " & i + 1)
                    If ser.Table IsNot Nothing AndAlso ser.Table.TableType = clsTable.enumTableType.Calibration Then AddSeries(ser.ZedCurveMeas, "Series " & i + 1 & " (Meas)")
                Next
            ElseIf DataSource IsNot Nothing Then
                DataSource.Rewind()
                For Each s As String In DataSource.FieldNames
                    .Columns.Add(s, s)
                Next
                While DataSource.Read And DataSource.NumRead <= 10000 'only show first 1000 records of data table
                    Dim ar(DataSource.FieldNames.Count - 1) As String
                    For i As Integer = 0 To DataSource.FieldNames.Count - 1
                        ar(i) = DataSource.Items(i)
                    Next
                    .Rows.Add(ar)
                End While
            End If

            For c As Integer = 0 To dgTable.ColumnCount - 1
                .Columns(c).SortMode = DataGridViewColumnSortMode.NotSortable
            Next
        End With
    End Sub

    Private Sub AddSeries(ByVal crv As ZedGraph.LineItem, ByVal Caption As String)
        With dgTable
            .ColumnCount += 2
            .RowCount = Math.Max(.RowCount, crv.NPts)
            With .Columns(dgTable.ColumnCount - 2)
                .HeaderText = Caption & vbCr & Choose(GraphTab.GraphType + 1, "Date-Time", IIf(GraphTab.ProfileXPlot = clsGraphTab.enumProfileXPlot.RiverMile, "River Mile", "Segment No."), crv.Label.Text)
                .HeaderText = Caption
                Select Case GraphTab.GraphType
                    Case enumGraphType.TimeSeries
                        .HeaderText &= vbCr & "Date-Time"
                        .ValueType = GetType(DateTime)
                        .DefaultCellStyle.Format = MyDateTimeFormat()
                    Case enumGraphType.Profile
                        .HeaderText &= vbCr & IIf(GraphTab.ProfileXPlot = clsGraphTab.enumProfileXPlot.RiverMile, "River Mile", "Segment No.")
                        .ValueType = GetType(Double)
                        .DefaultCellStyle.Format = IIf(GraphTab.ProfileXPlot = clsGraphTab.enumProfileXPlot.RiverMile, "N2", "N1")
                    Case enumGraphType.Depth
                        .HeaderText &= vbCr & crv.Label.Text
                        .ValueType = GetType(Double)
                        .DefaultCellStyle.Format = "N2"
                    Case Else
                        .HeaderText &= vbCr & ActiveGraphTab.Zed.GraphPane.XAxis.Title.Text
                        .ValueType = GetType(Double)
                        .DefaultCellStyle.Format = "N4"
                End Select
            End With
            With .Columns(dgTable.ColumnCount - 1)
                Select Case GraphTab.GraphType
                    Case enumGraphType.TimeSeries
                        .HeaderText = Caption & vbCr & crv.Label.Text
                        .ValueType = GetType(Double)
                        .DefaultCellStyle.Format = "N3"
                    Case enumGraphType.Profile
                        .HeaderText = Caption & vbCr & crv.Label.Text
                        .ValueType = GetType(Double)
                        .DefaultCellStyle.Format = "N3"
                    Case enumGraphType.Depth
                        .HeaderText = Caption & vbCr & "Depth"
                        .ValueType = GetType(Double)
                        .DefaultCellStyle.Format = "N2"
                    Case enumGraphType.Scatter
                        .HeaderText = Caption & vbCr & ActiveGraphTab.Zed.GraphPane.YAxis.Title.Text
                        .ValueType = GetType(Double)
                        .DefaultCellStyle.Format = "N4"
                    Case Else
                        .HeaderText = Caption & vbCr & crv.Label.Text
                        .ValueType = GetType(Double)
                        .DefaultCellStyle.Format = "N4"
                End Select
            End With
            For i As Integer = 0 To crv.NPts - 1
                Dim x As Double = crv.Points(i).X
                Dim y As Double = crv.Points(i).Y
                .Item(.ColumnCount - 2, i).Value = IIf(GraphTab.GraphType = enumGraphType.TimeSeries, Date.FromOADate(x), x)
                .Item(.ColumnCount - 1, i).Value = y
            Next
        End With
    End Sub
End Class